iT邦幫忙

2024 iThome 鐵人賽

DAY 4
0

Current Sprint: 1. 完成 HappyPath: 可以建立一場遊戲,玩到結束

今天要來點實作

分析一下要在網站上完成一場遊戲,需要哪些組件

  • UI 負責讓使用者操作
  • 後端應用程式保存遊戲狀態,處理遊戲規則邏輯
  • Database 保存紀錄
  • Redis 使用 websocket 連線機制時,實現 Pub/Sub 功能
  • 部署主機 提供服務給網路上的使用者

第一次技術選型

對於這些組件,考慮個人技術、快速開發的目的,選擇如下:

  • UI:僅開放 api endpoint,可用 curl、postman、或是 swagger 與後端互動
  • 後端:選擇 Rails,沒特別原因只是自己熟悉
  • Database:POSTGRESql
  • Redis:現階段先不提供 websocket 即時連線機制
  • 部署主機:選用 zeabur

需求

來定義第一次需求吧

  1. 「玩家」可以建立一場遊戲
    1. POST /api/v1/games
    2. body: ''
    3. response:
      // 201
      {
          "message": "成功建立遊戲",
          "game_id": 1
      }
      
  2. 「玩家」可以選擇職業:礦工
    1. POST /api/v1/games/{id}/play
    2. body:
      {
          "action": "選擇職業", // 之後會定義 Enum
          "role": "礦工", // 之後會定義 Enum
      }
      
    3. response:
      // 200
      {
          "message": "選擇職業礦工成功"
      }
      

實作

開專案

下面實作會以 Rails 語言為主,想一起做的小夥伴可以跟上 XD

  1. 開專案:https://github.com/side-project-at-SPT/ithome-ironman-2024-san-juan
  2. 開新的 Rails 專案
    $ ruby -v # ruby 3.3.4 (2024-07-09 revision be1089c8ec) [arm64-darwin23]
    $ rails -v # Rails 7.2.1
    
    $ rails new ithome-ironman-2024-san-juan \
    --database=postgresql \
    --skip-action-mailer \
    --skip-test # will use RSpec later \
    --api # use API mode \
    --devcontainer
    $ cd ithome-ironman-2024-san-juan
    $ rails s
    

db 預設會去找 localhost:5432
本地沒有的話,可以用 devcontainer 開啟需要用的 db

  1. 訪問 localhost:3000
    https://ithelp.ithome.com.tw/upload/images/20240905/20150987sSvkcsH2mX.png
  2. 按下 Create database
    https://ithelp.ithome.com.tw/upload/images/20240905/20150987uAP7tvbghm.png
  3. 修改 config/route.rb
Rails.application.routes.draw do
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
  # Can be used by load balancers and uptime monitors to verify that the app is live.
  get "up" => "rails/health#show", as: :rails_health_check

  # Defines the root path route ("/")
  # root "posts#index"
  
  # 加上這行  
  root "rails/health#show"
end
  1. push commit

部署到 zeabur

  1. zeabur 開啟專案

  2. 選擇從 GitHub https://ithelp.ithome.com.tw/upload/images/20240905/20150987mV6xKmdZXI.png

  3. 選擇專案 https://ithelp.ithome.com.tw/upload/images/20240905/20150987pRBoFaGdS5.png

  4. 確認使用 dockerfile 部署 https://ithelp.ithome.com.tw/upload/images/20240905/20150987I7ZFff9R23.png

  5. 新增 postgresql

  6. 設定環境變數 https://ithelp.ithome.com.tw/upload/images/20240905/20150987BPI2UnHLLo.png

    • RAILS_MASTER_KEY
      • 在 config/master.key
    • DATABASE_URL
      • ${POSTGRES_URI}
  7. 重新部署 https://ithelp.ithome.com.tw/upload/images/20240906/20150987STIyoa2VN3.png

  8. 設定 DNS https://ithelp.ithome.com.tw/upload/images/20240906/20150987nDAtxF50H4.png

  9. 部署好後前往設定的網址,看到一片綠就 ok 啦

小結

比想像中花時間XD 待會再來更新圖片 已更新

editor 上傳圖片有夠難用 /images/emoticon/emoticon10.gif

明天要做什麼

  1. 完成 HappyPath

以上不代表明天會做,如有雷同純屬巧合


工商服務

SPT (Side Project Taiwan) 的宗旨是藉由Side Project開發來成就自我,透過持續學習和合作,共同推動技術和專業的發展。我們相信每一個參與者,無論是什麼專業,都能在這個社群中找到屬於自己的成長空間。

歡迎所有對Side Project開發有興趣的人加入我們,可以是有點子來找夥伴,也可以是來尋找有興趣的Side Project加入,邀請大家一同打造一個充滿活力且有意義的技術社群!

Discord頻道連結:https://sideproj.tw/dc


上一篇
Day 03 - 事件風暴 Event Storming
下一篇
Day 05 - 加入 swagger
系列文
透過實作網頁遊戲練習網站工程師的基本素養,以 San Juan(聖胡安) 為例。6
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言